<script lang="ts" setup>
import { ref } from 'vue'
import { defineExpose } from 'vue';
const emit = defineEmits(['update'])
const dialogVisible = ref(false)
const formValue = ref({
  username: '',
  age: '',
  gender: '',
  phone: '',
  address: '',
  isDisabled: false,
})
const title = ref('')
//判断是新增还是编辑
let isRdit: any = null
//打开弹框
const openDialog = (row: any) => {
  isRdit = row
  //清空表单
  dialogVisible.value = true
  if (row) {
    title.value = '编辑'
    Object.assign(formValue.value, row)
  } else {
    title.value = '新增'
    Object.assign(formValue.value, {
      username: '',
      age: '',
      gender: '',
      phone: '',
      address: '',
      isDisabled: false,
    })
  }
}
//点击确认
const confirm = () => {
  dialogVisible.value = false
  //子传父
  emit('update', { ...formValue.value }, isRdit)
}
//暴露方法
defineExpose({
  openDialog
})
</script>

<template>
  <el-dialog v-model="dialogVisible" :title="title" width="500">
    <el-form :model="formValue" label-width="auto" style="max-width: 600px">
      <el-form-item label="用户名">
        <el-input v-model="formValue.username" />
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="formValue.age" />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="formValue.gender" placeholder="select">
          <el-option label="男" value="男" />
          <el-option label="女" value="女" />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formValue.phone" />
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="formValue.address" />
      </el-form-item>
      <el-form-item label="是否禁用">
        <el-switch v-model="formValue.isDisabled" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss"></style>